package com.tp.service;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.tp.base.BaseService;
import com.tp.base.Paginator;
import com.tp.model.sys.SysAuthUser;
import com.tp.model.sys.SysOrg;
import com.tp.model.sys.SysUser;
import com.tp.model.sys.SysUserRole;
import com.tp.utils.DateUtils;
import com.tp.utils.SQLUtils;
import com.tp.utils.StrUtils;
import com.tp.utils.WebUtils;

/**
 * 用户service
 * 
 * @author TP
 *
 */
public class UserService extends BaseService {

	private static SysUser su = new SysUser().dao();
	private static SysAuthUser sau = new SysAuthUser().dao();

	/**
	 * 根据登录名查询用户
	 * 
	 * @param username
	 * @return
	 */
	public SysUser getUserByAccount(String account) {
		SysUser user = su.findFirstByWhere(" where logn_acct = ? and user_type in (" + WebUtils.USER_TYPE_ADMIN + ","
				+ WebUtils.USER_TYPE_NORMAL + ")", account);
		return user;
	}

	/**
	 * 分页查询
	 * 
	 * @param model
	 * @param orderBy
	 * @param paginator
	 * @return
	 */
	public Page<SysUser> getPageByWhere(SysUser model, String orderBy, Paginator paginator) {
		// 过滤掉系统管理员
		SQLUtils sql = new SQLUtils("from " + SysUser.tableName + " t left join " + SysOrg.tableName
				+ " o on o.id = t.org_id where t.id != 1");
		if (model.getAttrValues().length != 0) {
			sql.whereLike("logn_acct", model.getStr("logn_acct"));
			sql.whereLike("real_name", model.getStr("real_name"));
			sql.whereEquals("user_type", model.getInt("user_type"));
			sql.whereEquals("org_id", model.getInt("org_id"));
		}
		if (StrUtils.isEmpty(orderBy)) {
			sql.append(" order by t.id desc");
		} else {
			sql.append(" order by ").append(orderBy);
		}
		Page<SysUser> page = su.paginate(paginator, "select t.*,o.name as org_name", sql.toString());
		return page;
	}

	/**
	 * 删除用户
	 * 
	 * @param ids
	 * @param uid
	 * @return
	 */
	public boolean deleteObject(String[] ids, Integer uid) {
		if (!StrUtils.empty(ids)) {
			boolean flag = false;
			for (String id : ids) {
				SysUser model = new SysUser();
				model.put("update_id", uid);
				flag = model.deleteById(id);
				if (flag) {
					// 删除用户角色
					Db.update("delete from " + SysUserRole.tableName + " where user_id = ?", id);
					// 删除用户操作权限
					Db.update("delete from " + SysAuthUser.tableName + " where user_id = ?", id);
				}else{
					break;
				}
			}
			return flag;
		}
		return false;
	}
	
	/**
	 * 重置密码
	 * @param ids
	 * @param uid
	 * @return
	 */
	public boolean updataPwd(String[] ids, Integer uid) {
		if (!StrUtils.empty(ids)) {
			boolean flag = false;
			for (String id : ids) {
				SysUser model = new SysUser();
				model.setPassword(WebUtils.passwordEncrypt("123456"));
				model.setId(Integer.valueOf(id));
				model.put("update_id", uid);
				flag = model.update();
				if (!flag) {
					break;
				}
			}
			return flag;
		}
		return false;
	}

	/**
	 * 保存用户角色
	 * 
	 * @param userid
	 * @param roleids
	 * @param update_id
	 * @return
	 */
	public boolean saveUserRole(Integer userid, String roleids, int update_id) {
		boolean flag = false;
		// 删除用户原角色
		Db.update("delete from " + SysUserRole.tableName + " where user_id = ?", userid);
		if (StrUtils.isNotEmpty(roleids)) {
			String[] arry = roleids.split(",");
			for (String roleid : arry) {
				SysUserRole userRole = new SysUserRole();
				userRole.setUserId(userid);
				userRole.setRoleId(Integer.valueOf(roleid));
				// 日志添加
				userRole.put("update_id", update_id);
				userRole.put("update_time", DateUtils.getNow(DateUtils.DEFAULT_REGEX_YYYY_MM_DD_HH_MIN_SS));
				flag = userRole.save();
			}
		} else {
			flag = true;
		}
		return flag;
	}

	/**
	 * 保存用户操作权限
	 * 
	 * @param userid
	 * @param authids
	 * @param update_id
	 * @return
	 */
	public boolean saveUserAuth(Integer userid, String authids, int update_id) {
		boolean flag = false;
		SysAuthUser auser = sau.findFirstByWhere("where user_id=?", userid);
		if (StrUtils.empty(auser)) {
			auser = new SysAuthUser();
			auser.setUserId(userid.longValue());
			auser.setUrlAuth(authids);
			auser.setCreateTime(DateUtils.getNow(DateUtils.DEFAULT_REGEX_YYYY_MM_DD_HH_MIN_SS));
			flag = auser.save();
		} else {
			auser.setUrlAuth(authids);
			auser.setModifyTime(DateUtils.getNow(DateUtils.DEFAULT_REGEX_YYYY_MM_DD_HH_MIN_SS));
			flag = auser.update();
		}
		return flag;
	}
}
